package com.breez.aiauth.controller;

import com.breez.aiauth.common.config.handler.AiAuthException;
import com.breez.aiauth.common.util.R;
import com.breez.aiauth.domain.SysUser;
import com.breez.aiauth.domain.UserInfo;
import com.breez.aiauth.domain.dto.BindFaceDto;
import com.breez.aiauth.domain.dto.LoginDto;
import com.breez.aiauth.domain.dto.RegisterDto;
import com.breez.aiauth.service.AuthService;
import com.breez.aiauth.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RequestMapping("auth")
@RestController
@Api(tags = "用户身份验证、用户注册")
public class AuthController {
    @Autowired
    private AuthService authService;
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("人脸登录")
    @PostMapping("loginByFace")
    @ResponseBody
    public R loginByFace(@RequestBody HashMap<String, String> data) {
        String image = data.get("image");
        if (!StringUtils.hasText(image)) {
            return R.error("没有识别到人脸", 500);
        }
        JSONObject result = authService.loginByFace(image);
        System.out.println(result);
        int code = result.getInt("error_code");
        if (code == 0) {
            JSONObject jsonObject = result.getJSONObject("result").getJSONArray("user_list").getJSONObject(0);
            String userId = jsonObject.getString("user_id");
            SysUser user = sysUserService.getById(userId);
            if (user != null) {
                UserInfo userInfo = new UserInfo();
                BeanUtils.copyProperties(user, userInfo);
                return R.success(userInfo);
            } else {
                return R.error();
            }
        } else if (code == 223114) {
            return R.error("人脸太模糊", 223114);
        } else if (code == 223127) {
            return R.error(" 嘴巴遮挡过高", 223127);
        } else if (code == 223115) {
            return R.error("人脸光照不好", 223115);
        } else if (code == 222207) {
            return R.error("人脸未认证", 222207);
        } else if (code == 223113) {
            return R.error("人脸有被遮挡", 223113);
        } else if (code == 223116) {
            return R.error("人脸不完整", 223116);
        } else if (code == 223120) {
            return R.error("不是活体", 223120);
        } else if (code == 223121) {
            return R.error("左眼遮挡", 223121);
        } else if (code == 223122) {
            return R.error("右眼遮挡", 223122);
        } else if (code == 223123) {
            return R.error("左脸遮挡", 223123);
        } else if (code == 223124) {
            return R.error("右脸遮挡", 223123);
        } else {
            return R.error();
        }
    }

    @ApiOperation("账户登录")
    @PostMapping("loginByAccount")
    @ResponseBody
    public R loginByAccount(@RequestBody @Validated LoginDto loginDto) {
        SysUser sysUser = authService.loginByAccount(loginDto);
        if (sysUser != null) {
            UserInfo userInfo = new UserInfo();
            BeanUtils.copyProperties(sysUser, userInfo);
            return R.success(userInfo);
        } else {
            return R.error("登录失败", 500);
        }
    }

    @ApiOperation("用户注册")
    @PostMapping("register")
    @ResponseBody
    public R register(@RequestBody RegisterDto registerDto) throws AiAuthException {
        boolean register = authService.register(registerDto);
        return register ? R.success("注册成功", 0) : R.error("注册失败", 500);
    }

    @ApiOperation("绑定人脸")
    @PutMapping("bindFace")
    @ResponseBody
    public R bindFace(@RequestBody @Validated BindFaceDto bindFaceDto) {
        JSONObject register = authService.bindFace(bindFaceDto);
        System.out.println(register);
        int code = register.getInt("error_code");
        if (code == 0) {
            SysUser u = sysUserService.getById(bindFaceDto.getUserId());
            UserInfo userInfo = new UserInfo();
            BeanUtils.copyProperties(u, userInfo);
            return R.success(userInfo);
        } else if (code == 223114) {
            return R.error("人脸太模糊", 223114);
        } else if (code == 223127) {
            return R.error(" 嘴巴遮挡过高", 223127);
        } else if (code == 223115) {
            return R.error("人脸光照不好", 223115);
        } else if (code == 223113) {
            return R.error("人脸有被遮挡", 223113);
        } else if (code == 223116) {
            return R.error("人脸不完整", 223116);
        } else if (code == 223120) {
            return R.error("不是活体", 223120);
        } else if (code == 223121) {
            return R.error("左眼遮挡", 223121);
        } else if (code == 223122) {
            return R.error("右眼遮挡", 223122);
        } else if (code == 223123) {
            return R.error("左脸遮挡", 223123);
        } else if (code == 223124) {
            return R.error("右脸遮挡", 223123);
        } else {
            return R.error("认证失败", 500);
        }
    }
}
